package com.ming.controller;

import com.github.pagehelper.PageInfo;
import com.ming.pojo.entity.Emp;
import com.ming.pojo.entity.EmpExpr;
import com.ming.pojo.vo.EmpListVO;
import com.ming.pojo.dto.EmpQueryDTO;
import com.ming.pojo.vo.EmpVO;
import com.ming.pojo.vo.FileUploadVO;
import com.ming.pojo.vo.Result;
import com.ming.pojo.dto.LoginDTO;
import com.ming.pojo.vo.LoginVO;
import com.ming.service.EmpService;
import com.ming.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private FileService fileService;

    @GetMapping
    public Result list(EmpQueryDTO empQueryDTO) {
        log.info("分页查询员工，查询条件: {}", empQueryDTO);
        PageInfo<EmpListVO> pageInfo = empService.pageQuery(empQueryDTO);
        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询员工: {}", id);
        EmpVO empVO = empService.getById(id);
        return Result.success(empVO);
    }

    @PostMapping
    public Result add(@RequestBody EmpVO empVO) {
        log.info("新增员工: {}", empVO);
        Emp emp = new Emp();
        emp.setUsername(empVO.getUsername());
        emp.setPassword(empVO.getPassword());
        emp.setName(empVO.getName());
        emp.setGender(empVO.getGender());
        emp.setPhone(empVO.getPhone());
        emp.setJob(empVO.getJob());
        emp.setSalary(empVO.getSalary());
        emp.setImage(empVO.getImage());
        emp.setEntryDate(empVO.getEntryDate());
        emp.setDeptId(empVO.getDeptId());

        empService.add(emp, empVO.getExprList());
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody EmpVO empVO) {
        log.info("更新员工: {}", empVO);
        Emp emp = new Emp();
        emp.setId(empVO.getId());
        emp.setName(empVO.getName());
        emp.setGender(empVO.getGender());
        emp.setPhone(empVO.getPhone());
        emp.setJob(empVO.getJob());
        emp.setSalary(empVO.getSalary());
        emp.setImage(empVO.getImage());
        emp.setEntryDate(empVO.getEntryDate());
        emp.setDeptId(empVO.getDeptId());

        empService.update(emp, empVO.getExprList());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除员工: {}", id);
        empService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{empId}/exprs")
    public Result getExprByEmpId(@PathVariable Integer empId) {
        log.info("查询员工工作经历: {}", empId);
        List<EmpExpr> exprList = empService.getExprByEmpId(empId);
        return Result.success(exprList);
    }

    @PostMapping("/exprs")
    public Result addExpr(@RequestBody EmpExpr empExpr) {
        log.info("新增员工工作经历: {}", empExpr);
        empService.addExpr(empExpr);
        return Result.success();
    }

    @PutMapping("/exprs")
    public Result updateExpr(@RequestBody EmpExpr empExpr) {
        log.info("更新员工工作经历: {}", empExpr);
        empService.updateExpr(empExpr);
        return Result.success();
    }

    @DeleteMapping("/exprs/{id}")
    public Result deleteExpr(@PathVariable Integer id) {
        log.info("删除员工工作经历: {}", id);
        empService.deleteExprById(id);
        return Result.success();
    }

    /**
     * 员工头像上传
     * @param id 员工ID
     * @param file 头像文件
     * @return 上传结果
     */
    @PostMapping("/{id}/avatar")
    public Result uploadAvatar(@PathVariable Integer id, @RequestParam("file") MultipartFile file) throws IOException {
        log.info("员工 {} 上传头像: {}", id, file.getOriginalFilename());

        // 1. 上传文件
        FileUploadVO uploadResult = fileService.uploadFile(file);

        // 2. 查询员工信息
        EmpVO empVO = empService.getById(id);

        // 3. 构建更新对象，只更新头像字段
        Emp emp = new Emp();
        emp.setId(id);
        emp.setName(empVO.getName());
        emp.setGender(empVO.getGender());
        emp.setPhone(empVO.getPhone());
        emp.setJob(empVO.getJob());
        emp.setSalary(empVO.getSalary());
        emp.setImage(uploadResult.getFileUrl()); // 设置新的头像URL
        emp.setEntryDate(empVO.getEntryDate());
        emp.setDeptId(empVO.getDeptId());

        // 4. 更新员工信息（不包含工作经历）
        empService.update(emp, null);

        log.info("员工 {} 头像上传成功: {}", id, uploadResult.getFileUrl());
        return Result.success(uploadResult);
    }

    /**
     * 员工登录
     * @param loginDTO 登录信息
     * @return 登录结果（包含 token）
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("员工登录请求: {}", loginDTO.getUsername());
        LoginVO loginVO = empService.login(loginDTO);
        return Result.success(loginVO);
    }
}